package com.xonro.common.histrans.sfeg.biz.custom;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.exception.AWSIllegalArgumentException;
import com.actionsoft.sdk.local.SDK;
import com.alipay.sofa.common.profile.StringUtil;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.common.SfegRecordTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhuhj
 * @Description: 通用历史数据迁移 BO数据
 * @Date: 2023/12/28 14:34
 * @Version: 1.0
 */
public class CompanyPastBoApplyBiz extends SfegRecordTransBiz {
    //drp主表名
    public  static String  DrpName="";
    //数据库来源CCID连接1.0、2.0
    public  static String  CCID="";
    //数据来源
    public  static String  sourceDatabase="";
    //流程定义id
    public static String processDefId="";
    //流程节点id
    public static String processActivity="";
    //备注
    public static String remark="";
    //单据号
    public static String code="";
    //DRp数据标识主键
    public static String drpCode="";
    //drp流程实例id
    public static String drpProcess="";
    //映射表bindid
    public static String YsBindId="";
    //所有drp数据
    public static RowMap rowMap;
    //映射主表
    public  static   BO detail;
    //映射子表 属于主表的
    public  static   BO detailed;
    public  CompanyPastBoApplyBiz(RowMap map,BO bo,BO bos) {
        rowMap=map;
        detail=bo;
        detailed=bos;
            processDefId=detail.getString("PROCESSID");
            processActivity=detail.getString("PROCESSACTIVITY");
            remark=detail.getString("REMARK");
            YsBindId = detail.getBindId();
            //数据来源
            sourceDatabase = detail.getString("SOURCE_DATABASE");
            DrpName = detailed.getString("DRP_NAME");
            if (StringUtil.equals("1",sourceDatabase)) {
                CCID = DrpConst.ZZ_DRP_1_CCID;
                code="code";
                drpCode="ID";
            } else {
                CCID = DrpConst.ZZ_CCID;
                code="BillNo";
                drpCode="MyId";
            }
        }
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        SDK.getLogAPI().consoleInfo("进入222222++++++++");
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        List<RowMap> contentData = new ArrayList<>();
        //历史数据迁移映射表
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable(detailed.getString("BPM_NAME"));//项目基础信息
        logModel.setOuterTable(DrpName);//基础项目信息表
        logModel.setOuterPK(mainData.getString(drpCode));
        logModel.setOuterExt2(mainData.getString(code));//单据号
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBoName(detailed.getString("BPM_NAME"));
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        if (StringUtil.equals("2",sourceDatabase)) {
            mainBO.set("DATA_TYPE", "2");//历史数据
            mainBO.set("DRP_ID", mainData.getString("MyId"));
            mainBO.set("APPLY_NO", mainData.getString("BillNo"));
            mainBO.set("APPLY_DATE", mainData.getString("CreateTime"));
            mainBO.set("APPLY_USER", mainData.getString("CreateName"));
            mainBO.set("APPLY_USER_ID", mainData.getString("CreateId"));
            mainBO.set("APPLY_DEPT", mainData.getString("CreateDept"));
            mainBO.set("APPLY_DEPT_ID", mainData.getString("CreateDeptId"));
        }
        else {
            mainBO.set("DATA_TYPE",mainData.getString("2"));//历史数据
            mainBO.set("DRP_ID",mainData.getString("ID"));
            mainBO.set("APPLY_NO",mainData.getString("Code"));
            mainBO.set("APPLY_DATE", mainData.getString("CreateTime"));
            mainBO.set("APPLY_USER_ID", mainData.getString("CreateID"));
            mainBO.set("APPLY_DEPT_ID", mainData.getString("DeptID"));
        }
        //历史迁移子子表 主表的 非附件
        List<BO> list = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", detailed.getId()).addQuery("TYPE!=2", null).list();
        if (list != null && list.size() > 0) {
            for (BO bo : list) {
                mainBO.set(bo.getString("BPM_FIELD"), mainData.getString(bo.getString("DRP_FIELD")));
            }
        }
        if (StringUtil.equals("1", sourceDatabase)) {
            //历史数据迁移映射子表 子表的
            List<BO> list2 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", YsBindId).addQuery("IS_MAIN=", 0).list();
            if (list2 != null && list2.size() > 0) {
                for (BO bo : list2) {
                    contentData = SDK.getCCAPI().getRDSAPI(CCID).getMaps("SELECT * FROM " + bo.getString("DRP_NAME") + " WHERE ID = ?  ", mainData.getString("ID"));
                    for (RowMap content : contentData) {
                        String uuid = UUIDGener.getUUID();
                        BOCreateData subBOCreateData = new BOCreateData();
                        boCreateDataList.add(subBOCreateData);
                        subBOCreateData.setCreateUser(createUser);
                        subBOCreateData.setBoName(bo.getString("BPM_NAME"));
                        BO subBO = new BO();
                        subBO.setId(uuid);
                        subBOCreateData.setBo(subBO);
                        //历史迁移子子表 子表的 非附件
                        List<BO> boList = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE!=2", null).list();
                        if (boList != null && boList.size() > 0) {
                            for (BO bo1 : boList) {
                                subBO.set(bo1.getString("BPM_FIELD"), content.getString(bo1.getString("DRP_FIELD")));
                            }
                        }
                        super.addLogModel(historyTransContext, bo.getString("DRP_NAME"), content.getString("id"), bo.getString("BPM_NAME"), uuid, bindId);
                        //历史迁移子子表 子表的 附件
                        List<BO> boList1 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE=", 2).list();
                        if (boList1 != null && boList1.size() > 0) {
                            for (BO bos : boList1) {
                                FileTransData fileTransData = new FileTransData();
                                fileTransData.setBoId(mainBOId);
                                //OA表订单编号
                                //   fileTransData.setFkCode(mainData.getString("xmbh"));
                                fileTransData.setFileJson(content.getString(bos.getString("DRP_FIELD")));
                                fileTransData.setProcessInstId(bindId);
                                historyTransContext.getFileTransDatas().add(fileTransData);
                                fileTransData.setBoName(bo.getString("BPM_NAME"));
                                fileTransData.setFieldName(bos.getString("BPM_FIELD"));
                                // fileTransData.setUserId(mainData.getString("CreateId"));//编制人Id
                                fileTransData.setUserId("admin");
                                fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
                                fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
                            }
                        }
                    }
                }
            }
        }
        else {
            //历史数据迁移映射子表 子表的
            List<BO> list1 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", YsBindId).addQuery("IS_MAIN=", 0).list();
            if (list1 != null && list1.size() > 0) {
                for (BO bo : list1) {
                    contentData = SDK.getCCAPI().getRDSAPI(CCID).getMaps("SELECT * FROM " + bo.getString("DRP_NAME") + "  WHERE Wiid = ?  ", mainData.getString("Wiid"));
                    for (RowMap content : contentData) {
                        String uuid = UUIDGener.getUUID();
                        BOCreateData subBOCreateData = new BOCreateData();
                        boCreateDataList.add(subBOCreateData);
                        subBOCreateData.setCreateUser(createUser);
                        subBOCreateData.setBoName(bo.getString("BPM_NAME"));
                        BO subBO = new BO();
                        subBO.setId(uuid);
                        subBOCreateData.setBo(subBO);
                        //历史迁移子子表 子表的 非附件
                        List<BO> boList = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE!=", 2).list();
                        if (boList != null && boList.size() > 0) {
                            for (BO bo1 : boList) {
                                subBO.set(bo1.getString("BPM_FIELD"), content.getString(bo1.getString("DRP_FIELD")));
                            }
                        }
                        super.addLogModel(historyTransContext, bo.getString("DRP_NAME"), content.getString("id"), bo.getString("BPM_NAME"), uuid, bindId);
                        //历史迁移子子表 子表的 附件
                        List<BO> boList1 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE=", 2).list();
                        if (boList1 != null && boList1.size() > 0) {
                            for (BO bos : boList1) {
                                FileTransData fileTransData = new FileTransData();
                                fileTransData.setBoId(mainBOId);
                                //OA表订单编号
                                //   fileTransData.setFkCode(mainData.getString("xmbh"));
                                fileTransData.setFileJson(content.getString(bos.getString("DRP_FIELD")));
                                fileTransData.setProcessInstId(bindId);
                                historyTransContext.getFileTransDatas().add(fileTransData);
                                fileTransData.setBoName(bo.getString("BPM_NAME"));
                                fileTransData.setFieldName(bos.getString("BPM_FIELD"));
                                // fileTransData.setUserId(mainData.getString("CreateId"));//编制人Id
                                fileTransData.setUserId("admin");
                                fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
                                fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
                            }
                        }
                    }
                }
            }
        }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);
        //历史数据迁移映射子表 主表的 附件
        List<BO> boList = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", detailed.getId()).addQuery("TYPE=", 2).list();
        if (boList != null && boList.size() > 0) {
            for (BO bo : boList) {
                SDK.getLogAPI().consoleInfo("附件名称+++"+bo.getString("DRP_FIELD"));
                FileTransData fileTransData = new FileTransData();
                fileTransData.setBoId(mainBOId);
                //OA表订单编号
                //   fileTransData.setFkCode(mainData.getString("xmbh"));
                fileTransData.setFileJson(mainData.getString(bo.getString("DRP_FIELD")));
                fileTransData.setProcessInstId(bindId);
                historyTransContext.getFileTransDatas().add(fileTransData);
                fileTransData.setBoName(detailed.getString("BPM_NAME"));
                fileTransData.setFieldName(bo.getString("BPM_FIELD"));
                // fileTransData.setUserId(mainData.getString("CreateId"));//编制人Id
                fileTransData.setUserId("admin");
                fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
                fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
            }
        }
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return null;
    }

    @Override
    protected RowMap getMainData() {
        return rowMap;
    }

    @Override
    protected String getProcessDefId() {
        return processDefId;
    }

    @Override
    protected String getActivityDefId() {
        return processActivity;
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String billNo = ctx.getMainData().getString(code);//单据号
        return remark+billNo;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {

        RowMap mainData = ctx.getMainData();
        if (StringUtil.equals("1",sourceDatabase)) {
            drpProcess= mainData.getString("Code");
        }
        else {
            drpProcess=mainData.getString("Wiid");
        }
        //流程实例id
        return drpProcess;
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return DrpName;
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        //主键
        return mainData.getString(drpCode);
    }
}
